<?php
/**
 * 定时任务类
 * Created by PhpStorm.
 * User: huanglinjie
 * Date: 2019/4/23
 * Time: 9:03 AM
 */

namespace app\backstage\controller;


use app\common\exception\ApiException;
use app\model\GambleUserRecord;
use app\model\MasterPaymentLog;
use app\model\PaymentLog;
use app\model\SmsLog;
use app\model\UserAgentlog;
use app\model\UserProgram;
use app\util\ReturnCode;
use app\util\Tools;
use Pament\wxPament;
use think\Cache;
use think\cache\driver\Redis;
use think\console\command\make\Model;
use think\Controller;
use Tencent\SMS as Service;
use think\Db;
use think\Exception;
use think\Log;

class Crontab extends Controller
{

    const LIST_NAME = "group_ping_no_win_order";


    public function settleCrontab()
    {
        echo "####start####" . date("Y-m-d H:i:s") . "\n\n";

        Db::startTrans();
        try {

            $where = ['addDate' => date('Ymd')];
            $data = Db::name('user_award')->where($where)->find();
            if(isset($data['id'])) throw new Exception('昨日的任务已结算');
            //查询出名单
            $user_list = Db::name('user')
                ->field('userID, platformID')
                ->where(['isAllot' => 1,'statusCheck' => 1])
                ->select();

            $platform_m = [];
            $creat = [];
            foreach ($user_list as $item){
                echo $item['userID']."\n";
                $add = [
                    'type' => 1,
                    'status' => 2,
                    'addTime' => time(),
                    'addDate' => date('Ymd'),
                    'remark' => '对内任务奖励',
                    'month' => date('Ym'),
                ];
                $add['userID'] = $item['userID'];
                $add['platformID'] = $item['platformID'];
                if(isset($platform_m[$item['platformID']])){
                    $add['money'] = $platform_m[$item['platformID']];
                }else{
                    $platform = Db::name('platform')->where(['id' => $item['platformID']])->find();
                    if(!isset($platform['id'])) continue;
                    $platform_m[$item['platformID']] = $platform['money'];
                    $add['money'] = $platform['money'];
                }

                $creat[] = $add;

            }
            $flag = Db::name('user_award')->insertAll($creat);
            if(!$flag) throw new Exception('任务分佣失败');
            Db::commit();
        } catch (Exception $e) {
            echo $e->getMessage()."\n";
            Db::rollback();
        }
        echo "####end####" . date("Y-m-d H:i:s");
    }

    //用户分配操作员
    public function allot(){

        echo "####start####" . date("Y-m-d H:i:s") . "\n\n";

        Db::startTrans();
        try {

            $where = ['isAllot' => 1, 'statusCheck' => 1, 'newKm' => ''];
            $userIDs = Db::name('user')->where($where)->column('userID');
            var_dump($userIDs);exit();

            Db::commit();
        } catch (Exception $e) {
            echo $e->getMessage()."\n";
            Db::rollback();
        }
        echo "####end####" . date("Y-m-d H:i:s");
    }

    public function updatePass(){
        $list = config('pass.key');
        var_dump($list);
        $userList = Db::name('user')->select();
        foreach ($list as $item){
            foreach ($userList as $k => $v){
                Db::name('user')->where('userID', $v['userID'])->update([$item => random(8, 'all')]);
            }
        }
        echo  "success!!!";
    }
}
